<?php
// +----------------------------------------------------------------------
// | 幕小铺
// +----------------------------------------------------------------------
// | 版权所有 2016~2020 幕小铺科技（大连）有限公司
// +----------------------------------------------------------------------
// | 2020/03/14
// +----------------------------------------------------------------------

namespace app\user_center\model\user;

use think\Model;

/**
 * 用户手机绑定 模型
 * @package app\user_center\model
 */
class UcMemberMobile extends Model
{
    // 设置当前模型对应的完整数据表名称
    protected $table = 'xm_uc_member_mobile';

    /**
     * 获取用户信息
     * @param int $uid 用户ID
     * @param int $mobile 手机号
     * @author shiqiang <976162041@qq.com>
     * @return array
     */
	public static function getDataByUid($uid){
        $map = [];
        $map['uid'] = $uid;
        $info = self::where($map)->find();
        if(!empty($info)){
            return array_out(200, 'success', $info);
        }else{
            return array_out(-1001, 'error');
        }
    }
    public static function getDataByMobile($mobile){
        $map = [];
        $map['mobile'] = $mobile;
        $info = self::where($map)->find();
        if(!empty($info)){
            return array_out(200, 'success', $info);
        }else{
            return array_out(-1001, 'error');
        }
    }
	public static function getDataByCheck($uid,$mobile){
        $map = [];
        $map['uid']    = $uid;
        $map['mobile'] = $mobile;
        $info = self::where($map)->find();
        if(!empty($info)){
            return array_out(200, 'success', $info);
        }else{
            return array_out(-1001, 'error');
        }
    }
	
	/**
     * 添加用户手机号码
     * @param int $uid     用户id
     * @param int $mobile  手机号码
     * @author shiqiang <976162041@qq.com>
     * @return array
     */
	public function addData($uid, $mobile, $create_time = ''){
		$data['uid'] 		= $uid;
		$data['mobile'] 	= $mobile;
		if($create_time != '')$data['create_time'] 	= $create_time;
        $res = $this -> isUpdate(false) ->data($data, true) -> save();//添加  thinkphp主键bug
        if($res){
            return array_out(200, 'success');
        }else{
            return array_out(-1001, 'error');
        }
	}
	
	/**
     * 变更手机号码
     * @param int $uid     用户id
     * @param int $mobile  手机号码
     * @author shiqiang <976162041@qq.com>
     * @return array
     */
	public function updateDataByMobile($uid, $mobile, $mobile_token){
		$map = [];
		$map['mobile'] = $mobile;
		$res1 =  self::where('uid', $uid) ->update($map);
		
		$res2 = model('UcMemberMobileSendcode', 'model\user') -> useDataToken($mobile_token);
		if($res1 > 0 && $res2 > 0){
            return array_out(200, 'success');
		}else{
			return array_out(-1001, 'error');
		}
	}
    /**
     * 用户统计 关联载入（一对一）
     */
    public function getUcMember(){
        return $this -> hasOne('app\user_center\model\user\Member','uid', 'uid');
    }
}
